<?php

namespace App\Http\Controllers\Admin;
use App\Models\Common\CollOption;
use App\Models\Common\CollArea;
use App\Models\Personal\Resume;
use Illuminate\Http\Request;
use Style, Cache, DB;

class TalentReportController extends CommonController{
	/**
	 * 主页
	 * @return mixed
	 */
	public function talentReportList()
	{
		return Style::view('admin.safety.talent_report_list');
	}

	/**
	 * 省份人数
	 * @param Request $request
	 * @return array
	 */
	public function talentReportProvince(Request $request)
	{
		$result = DB::table('resume as a')
				->select(DB::raw('COUNT(cms_a.id) as total'),  DB::raw('left(cms_a.census,2) as length'),  'b.cid')
				->where('a.census', '>', 0)
				->whereIn('a.resume_status', [1, 2, 3]) 
				->leftJoin('coll_area as b', 'a.census', '=', 'b.cid')
				->groupBy('length')
				->get();

		$province = CollArea::where('grade',1)->get();
		$data     = array();
		foreach($province as $k=>$v){
			$total = 0;
			foreach($result as $k1=>$v1){
				if(mb_substr($v1->cid,0,2) == mb_substr($v->cid,0,2)){
				   $total = $v1->total;
				}
			}
			$data[$v->name] = $total;
		}
		asort($data);
		$title = array();
		$num = array();
		foreach($data as $k=>$v){
			$title[] = $k;
			$num[] = $v;
		}
		return ['title'=>$title, 'num'=>$num];
	}

	/**
	 * 学历分布
	 * @param Request $request
	 * @return array
	 */
	public function talentReportEducation(Request $request)
	{
		$result = Resume::select(DB::raw('COUNT(id) as total'), 'education')
				->where('education', '>', 0)
				->whereIn('resume_status', [1, 2, 3]) 
				->groupBy('education')
				->get();
		$title = array();
		$num = array();
		foreach($result as $k=>$v){
			 $title[] = CollOption::getOptionName($v->education,'education');
			 $num[] = $v->total;
		}
		return ['title'=>$title, 'num'=>$num];
	}

	/**
	 * 年龄分布
	 * @param Request $request
	 * @return array
	 */
	public function talentReportAge(Request $request)
	{
		//20岁以下
		$i1 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-20 year')))
		         ->count();

        //20-25岁
		$i2 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-25 year')))
		         ->where('birthday', '<=', date('Y-m-d',strtotime('-20 year')))
		         ->count();

		//25-30岁
		$i3 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-30 year')))
		         ->where('birthday', '<=', date('Y-m-d',strtotime('-25 year')))
		         ->count();

		//30-35岁
		$i4 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-35 year')))
		         ->where('birthday', '<=', date('Y-m-d',strtotime('-30 year')))
		         ->count();

		//35-40岁
		$i5 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-40 year')))
		         ->where('birthday', '<=', date('Y-m-d',strtotime('-35 year')))
		         ->count();

     	//40-50岁
        $i6 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '>', date('Y-m-d',strtotime('-50 year')))
		         ->where('birthday', '<=', date('Y-m-d',strtotime('-40 year')))
		         ->count();

 		//50岁以上
        $i7 = Resume::select('birthday')
		         ->whereIn('resume_status', [1, 2, 3])
		         ->where('birthday', '<', date('Y-m-d',strtotime('-50 year')))
		         ->count();

		$data = array(array('value'=>$i1, 'name'=>'20岁以下'),
					  array('value'=>$i2, 'name'=>'20-25岁'),
					  array('value'=>$i3, 'name'=>'25-30岁'),
					  array('value'=>$i4, 'name'=>'30-35岁'),
					  array('value'=>$i5, 'name'=>'35-40岁'),
					  array('value'=>$i6, 'name'=>'40-50岁'),
					  array('value'=>$i7, 'name'=>'50岁以上')
					);
		return ['data'=>$data];
	}
}